
type BusClass = {
    emit: (name: string) => void
        on: (name: string, callback: Function) => void}
type ParmasKey = string | number | symbol
type List = {
    [key: ParmasKey]: Array<Function>   //索引签名
} 

class bus implements BusClass {
    private list: List
    constructor() { 
        this.list={}
    }
    //发布
    emit(name: string,...args:Array<any>): void { 
        let eventName: Array<Function> = this.list[name]   //复制引入
        eventName.forEach((item: Function) => {
            item.apply(this,args)
        })
    }
    //订阅
    on(name: string, callback: Function): void { 
        let FunctionArray: Array<Function> = this.list[name] || []
        FunctionArray.push(callback)
        this.list[name]=FunctionArray
    }
}

//默认导出同一个实例
export default new bus()


